axios.defaults.baseURL='http://localhost:8080/'
//全局属性配置，在任意组件内可以使用this.$http获取axios对象
Vue.prototype.$http = axios;


//=========axios前置拦截器，确保发送axios请求携带token=========//
axios.interceptors.request.use(config=>{
    let token = localStorage.getItem("token")
    if(token){
        config.headers["token"] = token;
    }
    return config;
},error => {
    Promise.reject(error)
});
//=================================


//===========axios后置拦截器，处理noLogin错误===========
axios.interceptors.response.use(config=>{
    let data = config.data;
    if(!data.success && "noLogin"===data.message)
    {
        localStorage.removeItem("token");
        localStorage.removeItem("logininfo");
        location.href = "login.html";
    }
    return config;
},error => {
    Promise.reject(error)
});

//

//=============静态资源拦截器===================
if (location.href.indexOf("index.html") === -1
    && location.href.indexOf("login.html") === -1
    && location.href.indexOf("register.html") === -1
    && location.href.indexOf("binder.html") === -1
    && location.href.indexOf("callback.html") === -1
){
    let user = localStorage.getItem("logininfo");
    if(!user){
        //定位到登陆页面
        location.href = "login.html";
    }
}

/**
 * 解析url获取参数对象
 * @param url
 * @returns {{}}
 */
function parseUrlParams2Obj(url){
    //获取请求参数
    let paramStr = url.substring(url.indexOf("?")+1);//code=021FdXFa14mYlA016OGa1L3R4X0FdXFO&state=1
    let paramStrs = paramStr.split("&");//[code=021FdXFa14mYlA016OGa1L3R4X0FdXFO,state=1]
    let paramObj = {};
    for(let i=0;i<paramStrs.length;i++){
        let paramName = paramStrs[i].split("=")[0];//code
        let paramValue = paramStrs[i].split("=")[1];//021FdXFa14mYlA016OGa1L3R4X0FdXFO
        paramObj[paramName] = paramValue;
    }
    return paramObj;
}